<?php

namespace App\Http\Controllers;

use App\Common\Logger;
use App\Common\Sql;
use App\Common\Tool;

class PaiWeiReward extends Base
{

    public function logic()
    {
        $uid  = $this->uid;
        $row1 = Sql::select('t_player_info',[['id',$uid]]);
        $data = array();
        if (count($row1) > 0) {
            $data['uid']              = (int)$uid;
            $data['paiwei_score']     = (int)$row1[0]['paiwei_score'];
            $ret_arr                  = $this->cal_paiwei_rank($data['paiwei_score']);
            $data['paiwei_rank']      = $ret_arr["paiwei_rank"];
            $data['paiwei_rank_name'] = $ret_arr["paiwei_rank_name"];
        }
        $row2 = Sql::select('t_rank_conf');

        for ($i = 0; $i < count($row2); $i++) {
            $arr                    = array();
            $arr["id"]              = (int)$row2[$i]["id"];
            $arr["rank_name"]       = $row2[$i]["name"];
            $arr["rank_min_score"]  = (int)$row2[$i]["min_score"];
            $arr["rank_max_score"]  = (int)$row2[$i]["max_score"];
            $data["rank_rewards"][] = $arr;
        }
        return $this->returnData($data);
    }

    //将rankscore转换排位rank名称
    private function cal_paiwei_rank($paiwei_score)
    {
        $row = Sql::select('t_rank_conf',[['min_score','<=',$paiwei_score],['max_score','<',$paiwei_score]]);
        if (count($row) <= 0) {
            Logger::instance()->error(Tool::getUri(),"get paiwei error.\n");
            $rank                     = array();
            $rank['paiwei_rank']      = 0;
            $rank['paiwei_rank_name'] = "无";
            return $rank;
        }
        $rank                     = array();
        $rank['paiwei_rank']      = (int)$row[0]["id"];
        $rank['paiwei_rank_name'] = $row[0]["name"];
        return $rank;
    }

}
